﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
    internal static class GeneratorParser_Helper
    {
        #region internal methods

        internal static byte[] GenerateTelegramBytes_Calling(Telegram_Calling telegram)
        {
			byte[] telegramBytes = null;
            
			if (telegram.TelegramFunction != eTelegramFunction_Calling.SEND_NDAT)
			{
				telegramBytes = Generate_FixedLength_Calling(telegram);
			}
			else
			{
				if (telegram is Telegram_Calling_SEND_NDAT)
				{
					Telegram_Calling_SEND_NDAT telegramCallingSENDNDAT = telegram as Telegram_Calling_SEND_NDAT;
					telegramBytes = GenParse_SEND_NDAT.GenerateBytes(telegramCallingSENDNDAT);
				}
				else
				{
					// TODO: handle error
				}
			}
            return telegramBytes;
        }

        internal static byte[] GenerateTelegramBytes_Returning(Telegram_Returning telegram)
        {
			byte[] telegramBytes = null;

			if (telegram is Telegram_Returning_RESPOND_NDAT)
			{
				Telegram_Returning_RESPOND_NDAT telegramReturningRespondNdata = telegram as Telegram_Returning_RESPOND_NDAT;
				telegramBytes = GenParse_Returning_RESPOND_NDAT.GenerateBytes(telegramReturningRespondNdata);
			}
			else if (telegram is Telegram_Returning_CONFIRM_ACK)
			{
				Telegram_Returning_CONFIRM_ACK telegramReturningConfirmAck = telegram as Telegram_Returning_CONFIRM_ACK;
				telegramBytes = GenParse_Returning_CONFIRM_ACK.GenerateBytes(telegramReturningConfirmAck);
			}
			else
			{
				telegramBytes = Generate_FixedLength_Returning(telegram);
			}

            return telegramBytes;
        }

        internal static Telegram ParseTelegramBytes(byte[] telegramBytes)
        {
            if (telegramBytes != null)
            {
                if (telegramBytes.Length > 0)
                {
                    if (FixedLength.IsThisAFixedLengthTelegram(telegramBytes))
                    {
                        return FixedLength.Parse_FixedLengthTelegram(telegramBytes);
                    }
                    else
                    {
						return VariableLength.Parse_VariableLengthTelegram(telegramBytes);
                    }
                }
                else
                {
                    // TODO handle error
                    return null;
                }
            }
            else
            {
                // TODO handle error
                return null;
            }
        }

        #endregion internal methods

        #region private methods

        private static byte[] Generate_FixedLength_Calling(Telegram_Calling telegram)
        {
            byte controlField = ControlField.Generate_ControlField_Calling(telegram.Direction, telegram.FrameCountValid, telegram.FrameCountFlag, telegram.TelegramFunction);

            byte[] telegramBytes = FixedLength.Generate_FixedLengthTelegram(controlField, telegram.Address);

            return telegramBytes;
        }
		
		private static byte[] Generate_FixedLength_Returning(Telegram_Returning telegram)
        {
            byte controlField = ControlField.Generate_ControlField_Returning(telegram.Direction, telegram.AccessDemandFlag, telegram.DataFlowControlStopFlag, telegram.TelegramFunction);

            byte[] telegramBytes = FixedLength.Generate_FixedLengthTelegram(controlField, telegram.Address);

            return telegramBytes;
        }

        #endregion private methods
    }
}
